<?php

defined('_SECURITY_TOKEN') or die("caia fora");

class controllerlogin //extends AppController
{

	function __construct(  )
	{
		
		$task	=	( empty($_REQUEST['task'])? '':$_REQUEST['task']);
		
		switch( $task ){
			
			case 'form':
				
				application::executeView( $task );
				
				break;
			case 'create':
				
				application::executeView( $task );
				
				break;
			case 'save':
				
				echo $this->save( $_POST );
				application::executeView( 'webservice' );
				
				break;
			case 'authentication':
				
				echo $this->authentication( $_POST['email'], $_POST['password'] );
				application::executeView( 'webservice' );
				
				break;
			case 'logout':
				
				echo $this->logout( );
				application::executeView( 'restrict' );
				
				break;
			case 'restrict':
				
				application::executeView( $task );
				
				break;	
			default:
				
				echo "view não definida.";
				break;
		}
		
	}
	
	public function save( $args ){
			
		$result	=	2;		
		$model	=	application::getModel('user');
		
		if( $args['email']!='' && $args['password']!='' ){
			
			$result	=	$model->save( $args );
			
			if( $result )
				$result=1;
		}
		
		
		//application::setAttribute('save', '1');
		//else
			//$result=2;//application::setAttribute('save', '2');
		
		return $result;
		
	}
	
	public function authentication( $email, $password ){
				
		$model	=	application::getModel('user');
		
		$result	=	$model->authentication( $email, $password );
		
		if( $result )
			$result=1;//application::setAttribute('save', '1');
		else
			$result=2;//application::setAttribute('save', '2');
		
		return $result;	
		
	}
	
	public function logout( ){
				
		application::session_logout();
		
	}
	
}